package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.gui.Event;
import org.teamh.logic.datamodel.*;

public class UserAssetsHandler extends JsonMasterHandler {

	public UserAssetsHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}
	
	public boolean handleMsg(JsonEventObject json) {
		
		if (json.getSource().startsWith("UserAssets")) {
			
			if(!manager.getUserAssets().containsKey(json.getSource())) {
				manager.addUserAssets(new UserAssets(json.getSource()));
			}
			
			if (json.getProperty().equals("balance")) {
				int oldValue = 0;
				if(json.getOldValue() != null) {
					oldValue = Integer.parseInt(json.getOldValue());
				}
				int newValue = Integer.parseInt(json.getNewValue());
				manager.getUserAssets(json.getSource()).setBalance(newValue);
				if(newValue - oldValue > 0) {
					manager.getUserAssets(json.getSource()).addToBalanceForSimulation(newValue - oldValue);
				}
				start.getGui().addEvent(new Event("gold", json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("color")) {
				manager.getUserAssets(json.getSource()).setColor(json.getNewValue());
				return true;
			} else if (json.getProperty().equals("income")) {
				manager.getUserAssets(json.getSource()).setIncome(Integer.parseInt(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("lives")) {
				manager.getUserAssets(json.getSource()).setLives(Integer.parseInt(json.getNewValue()));
				start.getGui().addEvent(new Event("updateLives", ""));
				if (json.getOldValue() == null && Integer.parseInt(json.getNewValue()) != 0) {
					start.getGui().addEvent(new Event("lives", json.getNewValue()));
				}
				if (json.getOldValue() != null) {
					if (manager.getUserAssets(json.getSource()).getUser() == null) {
						return true;
					}
					if (manager.getUserAssets(json.getSource()).getUser().getNickname() == null) {
						return true;
					}
					if (manager.getUserAssets(json.getSource()).getUser().getNickname().equals(start.getModel().getPlayerName())) {
						start.getGui().addEvent(new Event("lives", json.getNewValue()));
					}
				}
				return true;
			} else if (json.getProperty().equals("user")) {
				UserAssets userAssets = manager.getUserAssets(json.getSource());
				if(userAssets == null) {
					userAssets = new UserAssets(json.getSource());
					manager.addUserAssets(userAssets);
				}
				
				if(json.getNewValue() != null) {
					User user = manager.getUser(json.getNewValue());
					if(user == null) {
						user = new User(json.getNewValue());
						manager.addUser(user);
					}
					userAssets.setUser(user);
				} else {
					userAssets.setUser(null);
				}
				
//				if (json.getNewValue() != null) {
//					if (manager.getUser(json.getNewValue()) != null) {
//						manager.getUserAssets(json.getSource()).setUser(manager.getUser(json.getNewValue()));
//						return true;
//					}
//					if (json.getOldValue() != null && json.getNewValue() == null) {
//						manager.getUserAssets(json.getSource()).setUser(null);
//						manager.removeUser(json.getOldValue());
//						return true;
//					}
//					User user = new User(json.getNewValue());
//					manager.addUser(user);
//					manager.getUserAssets(json.getSource()).setUser(manager.getUser(json.getNewValue()));
//				}
				return true;
			} else if (json.getProperty().equals("tile")) {
				if (json.getNewValue() != null) {
					manager.getUserAssets(json.getSource()).setTile(manager.getTile(json.getNewValue()));
					if (start.getGui().getCurrentScreen() == start.getGui().getIngameScreen()) {
						start.getGui().addEvent(new Event("tile", manager.getTile(json.getNewValue()).getPosition() + " "
							+ manager.getTile(json.getNewValue()).getID()));
					}	
				} else if (json.getOldValue() != null && json.getNewValue() == null) {
					manager.getUserAssets(json.getSource()).setTile(null);
				}
				return true;
			} else if (json.getProperty().equals("game")) {
				manager.getUserAssets(json.getSource()).setGame(start.getLogic().getGame());
				return true;
			} else if (json.getProperty().equals("visitor")) {
				manager.getUserAssets(json.getSource()).setVisitor(Boolean.parseBoolean(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("towers")) {
				if (json.getNewValue() == null && json.getOldValue() != null) {
					manager.getUserAssets(json.getSource()).removeTower(manager.getTower(json.getOldValue()));
					manager.removeTower(json.getOldValue());
				} else {
					manager.getUserAssets(json.getSource()).addTower(manager.getTower(json.getNewValue()));
				}
				return true;
			} else if (json.getProperty().equals("creeps")) {
				if (json.getNewValue() == null && json.getOldValue() != null) {
					manager.getCreep(json.getOldValue()).getCell().removeCreep(manager.getCreep(json.getOldValue()));
					manager.getUserAssets(json.getSource()).removeCreep(manager.getCreep(json.getOldValue()));
					manager.removeCreep(json.getOldValue());
				} else {
					manager.getUserAssets(json.getSource()).addCreep(manager.getCreep(json.getNewValue()));
				}
				return true;
			}
			return false;
		} else {
			return false;
		}	
	}
}